<?php

namespace IDock\Insurance\AICS\Response;

use IDock\Http\Response;
use IDock\Insurance\AICS\Response\AllAICSResponse;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\VehicleActualValueVisitor;

class VehicleActualValueResponse extends AllAICSResponse
{

    /**
     * @var VehicleActualValueVisitor $visitor
     */
    public $visitor;

    /**
     * @param $visitor
     */
    public function setVisitor($visitor): void
    {
        $this->visitor = $visitor;
    }

    public function printerInit(): Response
    {
        $vehicleActualValuePrinter = new VehicleActualValuePrinter();
        $vehiclePrice = $this->visitor->vehiclePrice;

        $vehicleActualValuePrinter->actualRate = '0.0060';

        $firstRegisterDate = $this->visitor->firstRegisterDate;

        $startDate = date('Y-m-d', strtotime($this->visitor->startDate));

        $monthDiff = $this->monthDiff($firstRegisterDate, $startDate);

        $actualValue = round($vehiclePrice * (1 - $monthDiff * $vehicleActualValuePrinter->actualRate));

        $actualValueMostMin = round($vehiclePrice * 0.2);

        $vehicleActualValuePrinter->actualValue = max($actualValue, $actualValueMostMin);

        $vehicleActualValuePrinter->actualRate = (string)($vehicleActualValuePrinter->actualRate * 100);

        $this->printer = $vehicleActualValuePrinter;

        return $this;
    }

    /**
     * 相差月份
     * @param $firstRegisterDate
     * @param $startDate
     * @return float|int|mixed|string
     */
    private function monthDiff($firstRegisterDate, $startDate)
    {
        $firstRegisterDate = explode('-', $firstRegisterDate);

        $startDate = explode('-', $startDate);

        $DYear = $startDate[0] - $firstRegisterDate[0];
        $DMonth = $startDate[1] - $firstRegisterDate[1];
        $DDay = $startDate[2] - $firstRegisterDate[2];

        $monthDiff = $DYear * 12 + $DMonth;

        if ($DDay < 0) {
            $monthDiff -= 1;
        }

        return $monthDiff;
    }
}
